package org.softmed.rest.admin;

import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;

import main.RESTEditor;

import org.softmed.rest.admin.DetailChooser.DetailLevel;
import org.softmed.rest.admin.config.ConfigurationActionPanel;
import org.softmed.rest.admin.interfaces.EventListener;
import org.softmed.rest.admin.interfaces.FileHandler;
import org.softmed.rest.admin.interfaces.SelectionPathChanged;
import org.softmed.rest.admin.module.ModuleEditor;
import org.softmed.rest.admin.module.ValidationException;

import restlet.description.convert.RESTConfigurationDescription;
import restlet.description.convert.RESTModuleDescription;


public class ModuleManager extends JPanel implements EventListener,
		SelectionPathChanged, DetailLevelListener {

	List<RESTModuleDescription> moduleDescriptions;
	List<ModuleEditor> editors = new ArrayList<ModuleEditor>();

	int counter = 0;
	private FileHandler fileHandler;
	private ConfigurationActionPanel actionPanel;

	public ModuleManager() {
		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

		// setPreferredSize(new Dimension(540, 25));
		// setMinimumSize(new Dimension(540, 25));
	}

	public void addNewModule() {
		++counter;
		RESTModuleDescription moduleDescription = new RESTModuleDescription();
		moduleDescription.setName("module " + counter);
		moduleDescriptions.add(moduleDescription);

		addModuleEditorForDescription(moduleDescription);
	}

	protected void addModuleEditorForDescription(
			RESTModuleDescription moduleDescription) {
		ModuleEditor editor = new ModuleEditor();
		editor.setModuleDescription(moduleDescription);
		editor.setRemoveModuleListener(this);
		editor.setFileHandler(fileHandler);
		addModuleEditor(editor);
	}

	private void addModuleEditor(ModuleEditor editor) {
		add(editor);
		editors.add(editor);
		editor.setDetailLevel(actionPanel.getDetailLevel());
		revalidate();
		repaint();
	}

	@Override
	public void listen(Object source) {
		ModuleEditor editor = (ModuleEditor) source;
		RESTModuleDescription description = editor.getModuleDescription();
		moduleDescriptions.remove(description);
		remove(editor);
		editors.remove(editor);

		revalidate();
		repaint();
	}

	public List<RESTModuleDescription> getModuleDescriptions() {
		return moduleDescriptions;
	}

	public void setModuleDescriptions(
			List<RESTModuleDescription> moduleDescriptions) {
		this.moduleDescriptions = moduleDescriptions;
		refresh();
		for (RESTModuleDescription moduleDescription : moduleDescriptions) {
			addModuleEditorForDescription(moduleDescription);
		}
	}

	public void refresh() {
		removeAll();

	}

	public void validateData() throws ValidationException {
		for (ModuleEditor moduleEditor : editors) {
			moduleEditor.validateData();
		}

	}

	public void selectionPathChanged(boolean directory, String name,
			String parent) {
		for (ModuleEditor moduleEditor : editors) {
			moduleEditor.selectionPathChanged(directory, name, parent);
		}

	}

	public void setFileHandler(FileHandler fileHandler) {
		this.fileHandler = fileHandler;
		for (ModuleEditor moduleEditor : editors) {
			moduleEditor.setFileHandler(fileHandler);
		}

	}

	public void close() {
		// moduleDescriptions = null;
		if (moduleDescriptions != null)
			moduleDescriptions.clear();
		editors.clear();
		removeAll();
		revalidate();
		repaint();
	}

	public void setDetailLevel(DetailLevel detailLevel) {
		detailLevelChanged(detailLevel);
		actionPanel.setDetailLevel(detailLevel);

	}

	@Override
	public void detailLevelChanged(DetailLevel detailLevel) {
		for (ModuleEditor moduleEditor : editors) {
			moduleEditor.setDetailLevel(detailLevel);
		}
	}

	public void setActionPanel(ConfigurationActionPanel configurationActionPanel) {
		actionPanel = configurationActionPanel;
	}

}
